package com.example.friendcash.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.friendcash.BaseApplication;
import com.example.friendcash.R;
import com.example.friendcash.activities.PickFriendsActivity;
import com.example.friendcash.database.core.Friend;
import com.example.friendcash.database.services.interfaces.IServiceFactory;

import java.util.List;

public class FriendsFragment extends Fragment {

    private final IServiceFactory serviceFactory = BaseApplication.getInstance().getServiceFactory();
    private List<Friend> friends;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.friends_fragment, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final long entityId = BaseApplication.getInstance().getEntityId();
        friends = serviceFactory.createFriendService().getAllFriends(entityId);
        final ListView listView = (ListView) getView().findViewById(R.id.friendsList);
        listView.setAdapter(friendsAdapter);
        //add header
        final LinearLayout headerLayout = (LinearLayout) getActivity().getLayoutInflater()
                .inflate(R.layout.add_friends_from_contacts, null);
        final TextView headerText = (TextView) headerLayout.findViewById(R.id.addFriendsFromContact);
        headerText.setText(getString(R.string.add_friends_from_contacts));
        headerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent = new Intent(getActivity(), PickFriendsActivity.class);
                startActivity(intent);
            }
        });
        listView.addHeaderView(headerLayout);
        // add footer
        final LinearLayout footerLayout = (LinearLayout) getActivity().getLayoutInflater()
                .inflate(R.layout.add_friends_from_contacts, null);
        final TextView textView = (TextView) footerLayout.findViewById(R.id.addFriendsFromContact);
        textView.setText(getString(R.string.add_friends_from_fc));
        footerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent = new Intent(getActivity(), PickFriendsActivity.class);
                startActivity(intent);
            }
        });
        listView.addFooterView(footerLayout);
    }

    private BaseAdapter friendsAdapter = new BaseAdapter() {

        private final int ITEM_FRIENDS = 0;

        @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
            if (getItemViewType(position) == ITEM_FRIENDS) {
                final View view = convertView;
                return view;
            }
            return convertView;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public Object getItem(int arg0) {
            return friends.get(arg0);
        }

        @Override
        public int getCount() {
            return friends.size();
        }

    };

}
